﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace AUV5.Web.Pages
{
    public partial class MessageShow : PageBaseFrontend
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (base.IsVerified)
                {
                    //未读公告数
                    int bulletincount = BLL.BulletinBLL.Instance.GetUnReadBulletin(base.VerifiedUserID).Count;
                    if (bulletincount != 0)
                    {
                        lbBulletinCount.Text = bulletincount.ToString();
                        lbBulletinCount.Visible = true;
                    }

                    if (Request["userID"] != null)
                    {
                        string userid = Request["userID"];
                        userName.Text = BLL.UserInfoBLL.Instance.GetDetail(userid).UserName;

                        //不能给自己发送信息
                        if (userid == base.VerifiedUserID)
                        {
                            Response.Redirect(URL_INDEX);
                        }
                        //黑名单
                        //将别人加入黑名单
                        Model.BlackList spam = BLL.BlackListBLL.Instance.GetDetailByUserID(base.VerifiedUserID, userid);
                        if (spam != null)
                        {
                            panelSendMessage.Visible = false;
                            lbInBlackList.Text = string.Format("无法与{0}取得联系<br />您可以{1}", userName.Text, "<a class='bluetxt' href='" + Request.RawUrl + "&unspam=" + spam.ListID + "'>点此将他移出黑名单</a>");
                        }
                        //在别人的黑名单中
                        if (BLL.BlackListBLL.Instance.GetDetailByUserID(userid, base.VerifiedUserID) != null)
                        {
                            panelSendMessage.Visible = false;
                            lbInBlackList.Text = string.Format("{0}拒绝接收您的消息", userName.Text);
                        }

                        if (Request["unspam"] != null)
                        {
                            //将用户移出黑名单 
                            UnSpamUser(Request["unspam"]);
                            base.SuccessInfo = "移除成功，已恢复和\"" + userName.Text + "\"的联系";
                            Response.Redirect("MessageShow.aspx?userID=" + userid);
                        }
                    }
                    else
                    {
                        Response.Redirect("MessageBox.aspx");
                    }
                }
                else
                {
                    Response.Redirect(URL_LOGIN);
                }
            }
        }

        //移出黑名单
        private void UnSpamUser(string blacklistid)
        {
            BLL.BlackListBLL.Instance.Delete(blacklistid);
        }
        //生成消息列表
        public string GetMessageList()
        {
            int recordcount = 0;
            //读取消息列表并将消息设为已读
            IList<Model.Message> list = BLL.MessageBLL.Instance.ShowMessage(base.VerifiedUserID, Request["userID"], 100, 1, ref recordcount);


            StringBuilder sb = new StringBuilder();


            foreach (var item in list)
            {
                Model.UserInfo userfrom = BLL.UserInfoBLL.Instance.GetDetail(item.Sender);
                Model.UserInfo userto = BLL.UserInfoBLL.Instance.GetDetail(item.Receiver);
                if (userfrom != null && userto != null)
                {
                    string flt = "fr";
                    string bgcolor = "blue";
                    string dir = "left";
                    string sender = userfrom.UserName;
                    string receiver = "我";
                    if (item.Sender == base.VerifiedUserID)
                    {
                        sender = "我";
                        receiver = userto.UserName;
                        flt = "fl";
                        bgcolor = "grey";
                        dir = "right";
                    }
                    //管理员信息显示为红色
                    //else if (BLL.RoleInfoBLL.Instance.GetRoleName(BLL.UserInfoBLL.Instance.GetDetail(item.Sender).RoleID) == MANAGER)
                    //{
                    //    bgcolor = "red";
                    //}
                    sb.Append("<div class='dialoguelistinfo'>");

                    sb.Append(string.Format("<p class='{0} dialogueavatar'><img src='{1}' alt='' /></p>", flt, userfrom.PhotoPath));
                    sb.Append(string.Format("<p class='{0} text_{2} dialogueicon'><img src='/!images/icon_{1}.jpg' /></p>", flt, bgcolor == "blue" ? "dialogue" : bgcolor, dir));
                    sb.Append(string.Format("<div class='{0} dialoguetxt massageinfo{1}bg mar_{2}'>", flt, bgcolor, dir));
                    //相关服务
                    sb.Append(string.Format("<div class='relatedneeds'>{0}</div>", GetReferedInfo(item)));




                    //if (base.VerifiedUserID == item.Sender)
                    //{
                    sb.Append(string.Format("<div class='{0} '>", "positionr"));
                    sb.Append(string.Format("<span class='{0}'>{1}</span> 对 <span class='{2}'>{3}</span> 说：", sender == "我" ? "bluetxt bold" : "bold", sender, receiver == "我" ? "bluetxt bold" : "bold", receiver));
                    //举报信息
                    string informlink = item.Sender == base.VerifiedUserID ? "" : string.Format("<a  class='colorbox_report' href='/Pages/Inform.aspx?messageID={0}' target='_blank' alt=''>举报该信息</a>", item.MessageID);
                    sb.Append(string.Format("<p class='{0}'>{2}{1}</p>", sender == "我" ? "digtime" : "digtime", GetTimeSpan(item.CreateDate), informlink));
                    sb.Append("</div>");

                    sb.Append(string.Format("<p class='{0} dialogueinfowid'>", ""));
                    sb.Append(string.Format("\"{0}\"", item.Content));
                    sb.Append("</p>");
                    //}
                    //else
                    //{
                    //    sb.Append(string.Format("<p class='{0} dialogueinfowid'>", flt));
                    //    sb.Append(string.Format("“{0}”", item.Content));
                    //    sb.Append("</p>");

                    //    sb.Append(string.Format("<div class='{0}'>", flt));
                    //    sb.Append(string.Format("<span class='{0}'>{1}</span> 对 <span class='{2}'>{3}</span> 说：", sender == "我" ? "bluetxt bold" : "bold", sender, receiver == "我" ? "bluetxt bold" : "bold", receiver));
                    //    sb.Append("</div>");
                    //}

                    sb.Append("</div>");
                    sb.Append("<p class='clear'></p>");

                    sb.Append("</div>");
                }
            }

            return sb.ToString();
        }

        //发送消息
        protected void btnSendMessage_Click(object sender, EventArgs e)
        {
            if (Request["userID"] != null)
            {
                Model.Message message = new Model.Message();
                message.MessageID = Guid.NewGuid().ToString();
                if (txtContent.Text.Trim() != "")
                {
                    message.Content = this.txtContent.Text.Trim();
                }
                else
                {
                    lbMessage.Text = "信息不能为空！";
                    return;
                }
                message.RefServiceID = Request["serviceID"] ?? "";
                message.RefRequestID = Request["requestID"] ?? "";
                message.Sender = base.VerifiedUserID;
                message.Receiver = Request["userID"].ToString();
                message.ResourcePath = "";// hfpath.Value;
                message.IsRead = false;

                BLL.MessageBLL.Instance.SendMessage(message);

                this.txtContent.Text = "";

                base.SuccessInfo="发送成功！";

                Response.Redirect(Request.Url.ToString());
            }
            else
            {
                Response.Redirect(URL_LOGIN);
            }
        }

        //获取消息的引用信息
        protected string GetReferedInfo(Model.Message message)
        {
            string html = "";
            if (!string.IsNullOrEmpty(message.RefRequestID))
            {
                Model.RequestInfo request = BLL.RequestInfoBLL.Instance.GetDetail(message.RefRequestID);
                if (request != null)
                {
                    string requesttitle = request.Title;
                    //html = @"<a href='/Pages/RequestDetail.aspx?requestID=" + message.RefRequestID + "' target='_bland'>相关需求：" + requesttitle + "</a>";
                    html = @"相关需求：<a href='#'>" + requesttitle + "</a>";
                }
            }
            else if (!string.IsNullOrEmpty(message.RefServiceID))
            {
                Model.ServiceInfo service = BLL.ServiceInfoBLL.Instance.GetDetail(message.RefServiceID);
                if (service != null)
                {
                    string servicetitle = service.Title;
                    html = @"相关服务：<a href='/Pages/ServiceDetail.aspx?serviceID=" + message.RefServiceID + "' target='_bland'>" + servicetitle + "</a>";
                }
            }
            return html;
        }

    }
}